﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Data;

namespace PracticeProject
{
    public partial class Adding_Activity_Form : Form
    {
        public Activity activ;
        int  RelatedGoal = 0;
        public Adding_Activity_Form()
        {
            InitializeComponent();
            
        }

        public Adding_Activity_Form(int id)
        {
            InitializeComponent();
            RelatedGoal = id;
        }

        public Adding_Activity_Form(Activity activity)
        {
            InitializeComponent();
            activ = activity;
            RelatedGoal = activ.RelatedGoalId;
            TxtDesc.Text = activ.Description;
            TxtComments.Text = activ.Comments;
            DTEcpectingTime.Value = activ.ExpectedEndingTime;
            DTExpectingDate.Value = activ.ExpectedEndingTime;
            DTBeginingTime.Value = activ.StartingTime;
            DTBeginingDate.Value = activ.StartingTime;
            
        }

        private void Adding_Activity_Form_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime begindate = DTBeginingDate.Value;
                begindate = begindate.Add(-begindate.TimeOfDay);
                begindate = begindate.Add(DTBeginingTime.Value.TimeOfDay);
                DateTime enddate = DTExpectingDate.Value;
                enddate = enddate.Add(-enddate.TimeOfDay);
                enddate = enddate.Add(DTEcpectingTime.Value.TimeOfDay);
                if (enddate <= begindate)
                    throw new Exception();
                ElementCollection elements = ElementCollection.OpenCollectionFromXml();
                if (activ == null)
                    elements.AddActivity(TxtDesc.Text, TxtComments.Text, enddate, begindate, RelatedGoal);
                else
                {
                    activ.Description = TxtDesc.Text;
                    activ.Comments = TxtComments.Text;
                    activ.ExpectedEndingTime = enddate;
                    activ.StartingTime = begindate;

                }
                elements.SaveCollectionToXml();
                Close();
            }
            catch
            {
                MessageBox.Show("Ending time is equel or less then starting time", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void DTExpectingTime_ValueChanged(object sender, EventArgs e)
        {

        }

       
    }
}